
package code.codeworld.record.browse_record.controller;


import code.codeworld.record.browse_record.model.BrowseRecord;
import code.codeworld.record.browse_record.model.vo.BrowseRecordVo;
import code.codeworld.record.browse_record.service.BrowseRecordService;
import code.codeworld.common.kits.Request;
import code.codeworld.common.kits.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * world控制器
 *
 * @author freecode
 * @date 2023/03/23 00:20
 */
@CrossOrigin
@RequestMapping(Request.CODEWORLD_WEB)
@RestController
@Validated
public class BrowseRecordCtl {
    @Resource
    private BrowseRecordService BrowseRecordService;


    /**
     * 添加BrowseRecord
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @PostMapping("browseRecord/add")
    public Result<String> add(@RequestBody @Valid BrowseRecord browseRecord, HttpServletRequest httpRequest) throws Exception {
        String ip = Request.getIpAddress(httpRequest);
        browseRecord.setCreateIp(ip);
        browseRecord.setUpdateIp(ip);
        String address = Request.getAddressByIP(ip);
        browseRecord.setCreateIpAddress(address);
        browseRecord.setUpdateIpAddress(address);
        return BrowseRecordService.add(browseRecord);
    }

    /**
     * 获取BrowseRecord分页
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @GetMapping("browseRecord/page")
    public Page<BrowseRecord> page(BrowseRecordVo browseRecordVo) {
        return BrowseRecordService.page(browseRecordVo);
    }

    /**
     * 获取BrowseRecord详情
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @GetMapping("browseRecord/detail")
    public Result<BrowseRecord> detail(@Valid BrowseRecord browseRecord) {
        return Result.data(BrowseRecordService.detail(browseRecord));
    }

    /**
     * 编辑BrowseRecord
     *
     * @author freecode
     * @date 2023/03/23 00:20
     */
    @PostMapping("browseRecord/edit")
    public Result<String> edit(@RequestBody @Valid BrowseRecord browseRecord) {
        if (browseRecord.getId() == null) {
            return Result.paramError();
        }
        return BrowseRecordService.edit(browseRecord);
    }

}
